package com.cgsd.heyu.ui.login;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.android.library.utils.StringUtils;
import com.android.library.utils.ToastHelper;
import com.cgsd.heyu.R;
import com.cgsd.heyu.common.UserModeConfig;
import com.cgsd.heyu.presenter.user.UserContract;
import com.cgsd.heyu.presenter.user.UserPresenter;
import com.cgsd.heyu.ui.BaseActivity;

/**
 * @author xiaoxing
 * Description 忘记密码
 * CreateData: 2020/11/2
 */
public class ForgetPasswordActivity extends BaseActivity<UserContract.Presenter> implements View.OnClickListener, TextWatcher, UserContract.LoginView {
    /**
     * 倒计时（60s）
     */
    private final long MILLISINFUTURE = 60 * 1000;
    /**
     * //步长（1s）
     */
    private final long COUNTDOWNINTERVAL = 1000;
    private CountDownTimer timer;
    private EditText edtPhone;
    private EditText edtCode;
    private EditText edtPassword;
    private EditText edtConfirm;
    private TextView btnGetInto;
    private TextView tvSendCode;
    private String phone;
    private String code;
    private String password;
    private String confirmPw;


    public static void actionStart(Activity activity){
        /**
         * 到达此页面可清空用户相关数据和关闭所有页面
         */
        Intent intent = new Intent(activity, ForgetPasswordActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected void initView() {
        new UserPresenter(this);
        edtPhone = findViewById(R.id.edt_phone);
        edtCode = findViewById(R.id.edt_v_code);
        btnGetInto = findViewById(R.id.tv_get_into);
        tvSendCode = findViewById(R.id.tv_send_code);
        edtPassword = findViewById(R.id.edt_password);
        edtConfirm = findViewById(R.id.edt_confirm_password);

        btnGetInto.setOnClickListener(this);
        tvSendCode.setOnClickListener(this);
        edtCode.addTextChangedListener(this);
        edtPhone.addTextChangedListener(this);
        edtPassword.addTextChangedListener(this);
        edtConfirm.addTextChangedListener(this);

    }

    @Override
    protected void initData() {
        timer = new CountDownTimer(MILLISINFUTURE, COUNTDOWNINTERVAL) {
            @SuppressLint("SetTextI18n")
            @Override
            public void onTick(long millisUntilFinished) {
                tvSendCode.setEnabled(false);
                tvSendCode.setTextColor(Color.parseColor("#9A9A9A"));
                tvSendCode.setText("剩余"+millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                tvSendCode.setEnabled(true);
                tvSendCode.setTextColor(ContextCompat.getColor(ForgetPasswordActivity.this,R.color.app_color));
                tvSendCode.setText(getString(R.string.send_v_code));
            }
        };
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_get_into:
                submit();
                break;
            case R.id.tv_send_code:
                String phone = edtPhone.getText().toString();
                if (StringUtils.isMobileNo(phone)){
                    showCommonSubmitDialog("");
                    mPresenter.getVerificationCode(phone,"3");
                }else {
                    showToast(getString(R.string.toast_phone_error));
                }
                break;
            default:
        }
    }


    /**
     * 刷新登录进入按钮样式
     */
    private void refreshBtn(){
        phone = edtPhone.getText().toString();
        code = edtCode.getText().toString();
        password = edtPassword.getText().toString();
        confirmPw = edtConfirm.getText().toString();
        if (StringUtils.isMobileNo(phone) && StringUtils.isNotEmpty(code)
                && StringUtils.isNotEmpty(password) && StringUtils.isNotEmpty(confirmPw)){
            btnGetInto.setEnabled(true);
        }else {
            btnGetInto.setEnabled(false);
        }
    }

    /**
     * 提交数据
     */
    private void submit(){
        if (code.length() <= 0){
            showToast("验证码错误");
            return;
        }
        if (!password.equals(confirmPw)){
            showToast("密码不一致");
            return;
        }
        showCommonSubmitDialog("");
        mPresenter.updatePassword(phone,password,code);
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        refreshBtn();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }


    @Override
    public void codeSuccess() {
        dismissCommonSubmitDialog();
        ToastHelper.showDefaultToast("发送成功");
        timer.start();
    }

    @Override
    public void loginSuccess(UserModeConfig userModeConfig) {
        showToast("修改成功");
        finish();
    }
}
